import type { PlasmoMessaging } from "@plasmohq/messaging"

import { useTokenStroe } from "~store/tokenStore"
import type {
  AccessToken,
  TokenState,
  TokenStateObj,
  TokenType
} from "~type/tokenType"

const handler: PlasmoMessaging.PortHandler<TokenType, TokenStateObj> = async (
  req,
  res
) => {
  console.log(req)
  console.log("sendToken.....")
  const unsube = useTokenStroe.subscribe(console.log)
  const tokenType = useTokenStroe.getState().tokenType
  const accessToken = useTokenStroe.getState().accessToken
  // console.log("tokentype", { aiSource, apiKey, secretKey })
  res.send({
    reqToken: tokenType,
    token: accessToken
  })
  console.log("sendToken end.....")
  unsube()
}

export default handler
